package com.itany.corejava.day18;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

//通过Class都西昂获得类里的信息
public class TestClass2 {

    public static void main(String[] args) throws ClassNotFoundException {

        Class c=Class.forName("com.itany.corejava.day18.Person");

        Package pack=c.getPackage();

        System.out.println("package "+pack.getName()+";");

        String cName=c.getName();

        cName=c.getSimpleName();

        System.out.print("public class "+cName+" ");

        Class sClass=c.getSuperclass();

        System.out.print("extends "+sClass.getSimpleName()+" ");

        Class[] interfaces=c.getInterfaces();

        System.out.print("implements ");
        for (Class iClass : interfaces) {
            System.out.print(iClass.getSimpleName()+" ");
        }

        System.out.println("{");

        Field[] fs=c.getFields();

        fs=c.getDeclaredFields();

        // System.out.println(fs.length);

        for (Field f : fs) {
            int mod=f.getModifiers();
            String modifier= Modifier.toString(mod);
            System.out.print("    "+modifier+" ");

            System.out.print(f.getType().getSimpleName()+" ");

            System.out.println(f.getName()+";");
        }

        System.out.println();

        Constructor[] cs=c.getDeclaredConstructors();

        for (Constructor con : cs) {
            int mod=con.getModifiers();
            String modifier=Modifier.toString(mod);
            System.out.print("    "+modifier+" ");
            System.out.print(c.getSimpleName()+"(");

            Class[] paramClasses=con.getParameterTypes();

            for (Class paramClass : paramClasses) {
                System.out.print(paramClass.getSimpleName()+" ");
            }

            System.out.print(") throws ");

            Class[] exceptionClasses=con.getExceptionTypes();

            for (Class exceptionClass : exceptionClasses) {
                System.out.print(exceptionClass.getSimpleName()+" ");
            }
            System.out.println("{}");

        }

        System.out.println();

        Method[] ms=c.getDeclaredMethods();

        for (Method m : ms) {
            int mod=m.getModifiers();
            String modifier=Modifier.toString(mod);
            System.out.print("    "+modifier+" ");

            System.out.print(m.getReturnType().getSimpleName()+" ");

            System.out.print(m.getName()+"(");

            Class[] paramClasses=m.getParameterTypes();

            for (Class paramClass : paramClasses) {
                System.out.print(paramClass.getSimpleName()+" ");
            }

            System.out.print(") throws ");

            Class[] exceptionClasses=m.getExceptionTypes();

            for (Class exceptionClass : exceptionClasses) {
                System.out.print(exceptionClass.getSimpleName()+" ");
            }
            System.out.println("{}");
        }



        System.out.println("}");


    }
}
